package com.hl.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.Heartk;
import com.hl.sfzy.service.HeartkService;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/heartk")
@CrossOrigin
public class HeartkController {
    @Autowired
    private HeartkService heartkService;
    @PostMapping("/add")
    public R add(@RequestBody Heartk heartk){
        int num = heartkService.add(heartk);
        if (num != 0){
            return R.ok("添加成功");
        }else {
            return R.fail("添加失败");
        }
    }
    @GetMapping("/find")
    public R find(@RequestParam(required = false,defaultValue = "1") int pageNum,
                  @RequestParam(required = false,defaultValue = "3") int pageSize){
        List<Heartk> heartks = heartkService.find(pageNum, pageSize);
        return R.ok(heartks);
    }
    @GetMapping("/findByPage")
    public R findByPage(@RequestParam(required = false,defaultValue = "1") int pageNum,
                        @RequestParam(required = false,defaultValue = "3") int pageSize,
                        Heartk heartk){
        PageInfo<Heartk> pageInfo = heartkService.findByPage(pageNum, pageSize, heartk);
        return R.ok(pageInfo);
    }
    @DeleteMapping("/{id}")
    public R removeByPrimaryKey(@PathVariable("id") Integer id){
        int num = heartkService.removeByPrimaryKey(id);
        if (num != 0){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    @DeleteMapping("/removeBatch")
    public R removeBatch(@RequestBody List<Long> ids){
        int num = heartkService.removeBatch(ids);
        if (num!= 0){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    @PutMapping("/edit")
    public R editByPrimaryKey(@RequestBody Heartk heartk){
        int num = heartkService.editByPrimaryKey(heartk);
        if (num!= 0){
            return R.ok("修改成功");
        }else {
            return R.fail("修改失败");
        }
    }
    @GetMapping("/findByPrimaryKey")
    public R findByPrimaryKey(Long id){
        Heartk heartk = heartkService.findByPrimaryKey(id);
        return R.ok(heartk);
    }
}
